ggplot2 Time Series Heatmaps: revisited in the tidyverse

[This article was first published on MarginTale, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I revisited my previous post on creating beautiful time series calendar heatmaps in ggplot, moving the code into the tidyverse.
To obtain following example:


Simply use the following code:
# An simple function to turn an xts time series
# into a ggplot calendar heatmap
require(tidyverse)
# The core idea is to transform the data such that one can
# plot "Value" as a function of "WeekOfMonth" versus "DayOfWeek"
# and facet this Year versus Month
xts_heatmap <- function(x){
data.frame(Date=as.Date(index(x)), x[,1]) %>%
setNames(c("Date","Value")) %>%
dplyr::mutate(
Year=lubridate::year(Date),
Month=lubridate::month(Date),
# I use factors here to get plot ordering in the right order
# without worrying about locale
MonthTag=factor(Month,levels=as.character(1:12),
labels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),ordered=TRUE),
# week start on Monday in my world
Wday=lubridate::wday(Date,week_start=1),
# the rev reverse here is just for the plotting order
WdayTag=factor(Wday,levels=rev(1:7),labels=rev(c("Mon","Tue","Wed","Thu","Fri","Sat","Sun")),ordered=TRUE),
Week=as.numeric(format(Date,"%W"))
) %>%
# ok here we group by year and month and then calculate the week of the month
# we are currently in
dplyr::group_by(Year,Month) %>%
dplyr::mutate(Wmonth=1+Week-min(Week)) %>%
dplyr::ungroup() %>%
ggplot(aes(x=Wmonth, y=WdayTag, fill = Value)) +
geom_tile(colour = "white") +
facet_grid(Year~MonthTag) +
scale_fill_gradient(low="red", high="yellow") +
labs(x="Week of Month", y=NULL)
}
require(quantmod)
# Download some Data, e.g. the CBOE VIX
quantmod::getSymbols("^VIX",src="yahoo")
# lets see
xts_heatmap(Cl(VIX)) + labs(title="Heatmap of VIX")
# ok
I hope the commented code is self-explanatory – enjoy 🙂

To leave a comment for the author, please follow the link and comment on their blog: MarginTale.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)